package com.my.hzb.common.interceptor;

import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.my.hzb.common.bean.AuthorBean;
import com.my.hzb.common.bean.GlobalBean;
import com.my.hzb.common.bean.LoginUserBean;
import com.my.hzb.common.bean.ResultBean;
import com.my.hzb.common.util.CommonUtil;
import com.my.hzb.common.util.JsonUtil;
import com.my.hzb.common.util.MemcachedUtil;
import com.my.hzb.entiy.Menu;
//import com.my.hzb.entiy.Menu;
//import com.my.hzb.entiy.MenuRole;
//import com.my.hzb.entiy.Role;
//import com.my.hzb.entiy.UserRole;
//import com.my.hzb.user.service.MenuRoleService;
//import com.my.hzb.user.service.MenuService;
//import com.my.hzb.user.service.RoleService;
//import com.my.hzb.user.service.UserRoleService;

public class AuthorityInterceptor extends HandlerInterceptorAdapter{

	@Autowired
	private MemcachedUtil memcachedUtil;
//	@Autowired
//	private UserRoleService userRoleService;
//	@Autowired
//	private RoleService roleService;
//	@Autowired
//	private MenuRoleService menuRoleService;
//	@Autowired
//	private MenuService menuService;
	
	public final boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        try{
        	String url = request.getRequestURI();
	        
	        if(CommonUtil.isInterface(strList, url)){
	        	return true;
	        }
	        boolean flag = isAuthor(request);
	        if(flag){
	        	return true;
	        }
	        if (request.getHeader("accept") != null 
					&& request.getHeader("accept").equalsIgnoreCase("*/*")){
				//如果是ajax请求响应头会有x-requested-with
				System.out.println("ajax");
				ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,"还没有权限");
				result.setReturnObj("this is the url");
				String data = JsonUtil.objectToJson(result);  
		        OutputStream ps = response.getOutputStream();  
		        //这句话的意思，使得放入流的数据是utf8格式  
		        ps.write(data.getBytes("UTF-8"));
			}else{
				response.sendRedirect("/login.jsp");
			}
        }catch(Exception e){
        	e.printStackTrace();
        }
		return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)  {

    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)  {
    	
    }
    public boolean isAuthor(HttpServletRequest request){
    	String url = request.getRequestURI();
//    	UserRole userRole = new UserRole();
    	String cookie = CommonUtil.getCookieByName(request,GlobalBean.loginCookie);
    	if(null==cookie){
    		return false;
    	}
    	LoginUserBean cooUser = (LoginUserBean)JsonUtil.jsonToBean(cookie, LoginUserBean.class);
		if(null!=cooUser&&null!=cooUser.getUserName()){
			String author = (String)memcachedUtil.get("author_"+cooUser.getUserName());
			AuthorBean au = (AuthorBean)JsonUtil.jsonToBean(author, AuthorBean.class);
			if(null!=au&&null!=au.getAuthorList()){
				for(Menu menu:au.getAuthorList()){
					if(url.equals(menu.getMenuUrl())){
						return true;
					}
				}
			}
//			userRole.setUserName(cooUser.getUserName());
//	    	List<UserRole> userRoleList = userRoleService.find(userRole);
//	    	if(null!=userRoleList&&userRoleList.size()>0){
//	    		for(UserRole uRole:userRoleList){
//	    			Role role = roleService.getOne(uRole.getRoleId());
//	    			if(null!=role){
//	    				MenuRole menuRole = new MenuRole();
//	    				menuRole.setRoleName(role.getRoleName());
//	    				List<MenuRole> menuRoleList = menuRoleService.find(menuRole);
//	    				if(null!=menuRoleList&&menuRoleList.size()>0){
//	    					for(MenuRole mRole:menuRoleList){
//	    						Menu menu = menuService.getOne(mRole.getMenuId());
//	    						if(null!=menu){
//	    							if(url.equals(menu.getMenuUrl())){
//	    								return true;
//	    							}
//	    						}
//	    					}
//	    				}
//	    			}
//	    		}
//	    	}
    }
    return false;
    }
    
    private List<String> strList;

	public List<String> getStrList() {
		return strList;
	}
	public void setStrList(List<String> strList) {
		this.strList = strList;
	}
}
